/*
	OFWBooter is a homebrew that loads the official firmware of your current custom firmware
	Copyright (C) 2010  dxem
	
	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation; either
	version 2.1 of the License, or (at your option) any later version.
	
	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.
	
	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

 */

#include <pspsdk.h>
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspctrl.h>
#include <kubridge.h>

#define PSP_PHAT 0
#define PSP_SLIM 1
#define PSP_BRITE 2
#define PSP_GO 4

PSP_MODULE_INFO("ofwbooterv3", 0, 3, 0);
PSP_MAIN_THREAD_ATTR(PSP_THREAD_ATTR_USER);

#define printf pspDebugScreenPrintf

int dxemRebootOFW(void);

int main(int argc, char *argv[])
{
	pspDebugScreenInit();
	
	printf("OFW Booter v3\n");
	printf("\n\n");
	
	printf("Press X to Start OFW\n");
	printf("Press R to exit!\n");
	
	SceCtrlData pad;
	
	while(1)
	{
		sceCtrlReadBufferPositive(&pad, 1);
		if(pad.Buttons & PSP_CTRL_RTRIGGER)
		{
			printf("Exiting in 3 secounds.. Wait..\n");
			sceKernelDelayThread(3*1000*1000);
			sceKernelExitGame();
		};
		
		if(pad.Buttons & PSP_CTRL_CROSS)
		{
			SceUID mod = sceKernelLoadModule("ofwboot_kernel.prx", 0, NULL); /* Load ofwboot_kernel in Kernelmode */
			if(mod < 0)
			{
				printf("Error: could not load module 0x%08X\n", mod);
				sceKernelDelayThread(2*1000*1000);
				sceKernelExitGame();
			};
			
			mod = sceKernelStartModule(mod, argc, argv, 0, NULL);
			if(mod < 0)
			{
				printf("Error: could not start module 0x%08X\n", mod);
				sceKernelDelayThread(2*1000*1000);
				sceKernelExitGame();
			};
			
			break;
		};
	};
	
	int kernel = sceKernelDevkitVersion(); /* Get Devkit/Firmware Version */
	int model = kuKernelGetModel(); /* Get PSP Model */
	if(model == PSP_BRITE) /* if model is a Brite then */
	{
		model = PSP_SLIM; /* use Slim model */
	}
	else if(model == PSP_GO)
	{
		printf("Not testet on a Go :( .. Auto-Exit in 3 sec.\n");
		sceKernelDelayThread(3*1000*1000);
		sceKernelExitGame();
	};
	
	if(((kernel == 0x05000010) && (model == PSP_PHAT)) || ((kernel == 0x05050010) && (model == PSP_PHAT)) || ((kernel == 0x05050010) && (model == PSP_SLIM)) || (kernel < 0x05000010)) /* NON working Firmwares */
	{
		printf("This firmware version does not yet work with your model. Auto-Exit in 3 sec.\n");
		sceKernelDelayThread(3*1000*1000);
		sceKernelExitGame();
	};
	
	printf("Starting OFW.. Please Wait..\n");
	
	dxemRebootOFW(); /* Starting OFW commmand */
    sceKernelExitGame();
	return 0;
};

